package com.two.service.caipin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import com.two.common.entity.*;
import com.two.common.util.CommonResult;
import com.two.service.caipin.OpenFeign.CollectOpenFeign;
import com.two.service.caipin.OpenFeign.OpenFeignShopcar;
import com.two.service.caipin.OpenFeign.PinglunOpenfeign;
import com.two.service.caipin.OpenFeign.RestaurantOpenFeign;
import com.two.service.caipin.service.CaipinService;
import com.two.service.caipin.vo.*;
import com.two.service.shopcar.entity.Shopcar;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/caipin/caipin")
@CrossOrigin
@Api(tags = "菜品的控制层")
public class CaipinController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private CaipinService caipinService;
    @Autowired
    OpenFeignShopcar openFeignShopcar;

    @Autowired
    CollectOpenFeign collectOpenFeign;

    @Autowired
    PinglunOpenfeign pinglunOpenfeign;


    @PostMapping("findall")
    @ApiOperation(value = "查询菜品信息")
    public CommonResult test1(Integer currentPage,Integer pageSize){
       return caipinService.fingall(currentPage,pageSize);
    }

    //查询菜品的全部信息
    @PostMapping("caiFindAll")
    @ApiOperation(value = "查询菜品的全部i信息")
    public CommonResult caiFindAll(Integer currentPage,Integer pageSize ){
        return caipinService.selectcaiall(currentPage,pageSize);
    }

    //删除
    @PostMapping("delectByid")
    @ApiOperation("根据id删除菜品11")
    public CommonResult delectByid(String mid){
        int i = caipinService.delectByidq(mid);
        System.out.println("666666666666666666666"+mid);
        return new CommonResult(2000,"删除成功",i);
    }

    //修改
    @PostMapping("update")
    @ApiOperation("修改菜品信息")
    public CommonResult update(@RequestBody Menu menu){
        int update = caipinService.update(menu);
        if (update!=0){
            return new CommonResult(2000,"修改成功",update);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    //添加
    @PostMapping("insertcai")
    @ApiOperation("添加菜品")
    public CommonResult insertcai(@RequestBody Menu menu){
        int insert = caipinService.insertcai(menu);
        if (insert!=0){
            return new CommonResult(2000,"添加成功",insert);
        }
        return new CommonResult(5000,"添加失败",null);
    }

    //模糊查询
    @PostMapping("mohuAll")
    @ApiOperation("模糊查询")
    public CommonResult mohuAll(@RequestBody Menu menu,Integer currentPage,Integer pageSize){
        System.out.println(menu.getDishName()+"-----------------------------------");
        String disName = menu.getDishName();
        return caipinService.moselect(disName, currentPage, pageSize);
    }

    //查询待审核的菜名
    @PostMapping("shen")
    @ApiOperation("查询待审核的菜")
    public CommonResult shen(@RequestParam Integer currentPage, @RequestParam Integer pageSize,@RequestParam String token, @RequestBody RestaurantVo restaurantVo){
        return caipinService.shenselect(currentPage,pageSize,token,restaurantVo);
    }

    //


    @PostMapping("Phonegoods")
    @ApiOperation("手机加入购物车")
    public CommonResult phonegoods(String mid, Integer caiNumber, HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = stringRedisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(token1, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String usermonicker = user.getString("usermonicker");
        Menu goods = caipinService.goods(mid);
        Shopcar shopcars = new Shopcar();
        shopcars.setUsermonicker(usermonicker);
        shopcars.setDishName(goods.getDishName());
        shopcars.setDisPrice(goods.getPrice());
        shopcars.setRid(goods.getRid());
        shopcars.setMenuImg(goods.getMenuImg());
        shopcars.setNumber(goods.getNumber());
        shopcars.setDishNum(caiNumber);
        shopcars.setIsDeleted(String.valueOf(goods.getTai()));
        Shopcar[] shopcarss = {shopcars};
        //System.out.println(shopcars[0].toString());
        return openFeignShopcar.addGoods(token,shopcarss);
    }


    //手机端购物车 加号
    @PostMapping("phoneGou")
    @ApiOperation("购物车")
    public Menu phone(String mid){
        return caipinService.goods(mid);
    }


    //类别查询，
    //传入菜品的id   菜品表中没有菜品所在的餐厅
    //返回菜品id，名字，数量，饭店名
    @PostMapping("lei")
    @ApiOperation("类别菜品")
    public CommonResult lei(String mid){
        return caipinService.lei(mid);
    }

    //收藏查询，
    @PostMapping("selectAll")
    @ApiOperation("收藏查询")
    public PageInfo<Menu> selectAll(@RequestParam Integer currentPage,@RequestParam  Integer pageSize,@RequestParam  Integer userid, @RequestBody CollectVo collectVo){
        return caipinService.selectAll(currentPage,pageSize,userid,collectVo);
    }

    // 手机端收藏查询
    @PostMapping("CollectSelect")
    @ApiOperation("收藏查询")
    public List<Menu> CollectSelect(@RequestParam  Integer userid){
        return caipinService.collectSelect(userid);
    }

     //审核通过
    @PostMapping("pass")
    @ApiOperation("审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "审核人",name = "token",required = true),
            @ApiImplicitParam(value = "菜品id",name = "id",required = true)
    })
    public CommonResult pass(@RequestParam String token,@RequestParam String id){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        String usermonicker = user.getUsermonicker();
        System.out.println("昵称+——==============="+usermonicker);
        return caipinService.xiu(usermonicker,id);

    }

    //审核驳回
    @PostMapping("bohui")
    public CommonResult bohui(@RequestBody BohuiVo bohuiVo,@RequestParam String mid, @RequestParam String token){
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        String usermonicker = user.getUsermonicker();
        return caipinService.bo(usermonicker,bohuiVo,mid);
    }

    //类别删除
    //传入饭店id类别id
    @PostMapping("leiDelete")
    @ApiOperation("删除饭店的类别")
    public CommonResult leiDelete(@RequestBody DelectLeiVo delectLeiVo){

        return caipinService.leiDelete(delectLeiVo);
    }

    //查询菜系对应的餐厅，
    @PostMapping("leicname")
    @ApiOperation("餐厅菜系")
    public CommonResult leicname(@RequestParam Integer currentPage, @RequestParam Integer pageSize){
        System.out.println(pageSize+"-------------------------------------------------");
        return caipinService.leicname(currentPage,pageSize);
    }

    //菜系的菜
    //传入菜系，餐厅id,返回菜品   特价菜的价格不在price里面在dis_price  查询的有两个价格，在前端显示特价菜价格
    @PostMapping("caicname")
    @ApiOperation("类别餐厅名菜品")
    public CommonResult caicname(Integer cid,Integer rid){
        return caipinService.caicname(cid,rid);
    }


    //类别菜单的修改，修改的是表中的菜系的名子，传入菜系的id,修改的菜系名
    @PostMapping("updateleiname")
    @ApiOperation("修改类别名称")
    public CommonResult updateleiname(Integer cid,String style){
        return caipinService.updateleiname(cid,style);
    }

    // 收藏按钮
    @PostMapping("selectInsert")
    @ApiOperation("收藏菜品")
    public CommonResult selectInsert(Integer mid,String token){
        System.out.println(token);
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer userid = user.getUserid();
        Collect collect = new Collect();
        collect.setMid(mid);
        collect.setIsdeleted(0);
        collect.setUserid(userid);
        return collectOpenFeign.selectInsert(collect);
    }

    //查询所有的菜品
    @GetMapping("allxi")
    @ApiOperation("查询所有菜系")
    public CommonResult allxi(){
        return caipinService.allxi();
    }

    @Autowired
    private RestaurantOpenFeign restaurantOpenFeign;

    //查询所有的餐厅
    @GetMapping("allcan")
    @ApiOperation("查询所有餐厅")
    public CommonResult allcan(){
        return restaurantOpenFeign.selectAll();
    }

    //传入餐厅id，返回餐厅菜品类别
    @PostMapping("cidName")
    @ApiOperation("餐厅具有的菜系")
    public CommonResult cidName(Integer rid){
        return caipinService.cidName(rid);
    }

    //餐厅id查询餐厅的菜品
    @PostMapping("ridAllCaipin")
    @ApiOperation("餐厅具有的菜品，模糊查询,排序")
    public CommonResult ridallcaipin(Integer rid,Integer cid,String dishname,HttpServletRequest request){
        String token = request.getHeader("token");
        String token1 = stringRedisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(token1, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String usermonicker = user.getString("usermonicker");
        return caipinService.ridallcaipin(rid,cid,dishname,usermonicker);
    }

    //类别查看
    @PostMapping("selectlei")
    @ApiOperation("菜系id,餐厅id，查询菜品")
    public CommonResult selectlei(@RequestBody XicanVo xicanVo){
        return caipinService.selectlei(xicanVo);
    }

    //添加菜系
    @PostMapping("insertcaixi")
    @ApiOperation("添加菜系")
    public CommonResult insertcaixi(@RequestBody Cookstyle cookstyle){
        return caipinService.insertcaixi(cookstyle);
    }

    //修改菜系的名称
    @PostMapping("updatecaixi")
    @ApiOperation("修改菜系名称")
    public CommonResult updatecaixi(@RequestBody Cookstyle cookstyle){
        return caipinService.updatecaixi(cookstyle);
    }

    //评论

    @PostMapping("mselect")
    @ApiOperation("评论")
    public CommonResult mselect(String mid,Integer rid){
        CommonResult mselect = caipinService.mselect(mid, rid);
        Menu menuPing = (Menu) mselect.getData();
        CommonResult select = pinglunOpenfeign.select(menuPing.getDishName(), menuPing.getRid());
        List<Pinglun> list = (List<Pinglun>) select.getData();
        System.out.println("===================>"+list);
        menuPing.setPinglunList(list);
        if (list!=null){
            return new CommonResult(2000,"查询成功",menuPing);
        }
        return new CommonResult(5000,"查询失败",null);
    }
}
